﻿using Mb数据插入.Application.DataInsert.Dtos;
using SqlSugar.Extensions;
using System;
using WebFirst.Entities;
using YOUYOU.Models.DTO.HealthFollow;
using YOUYOU.Models.Entities.DBModels;

namespace Mb数据插入.Application
{
    public interface ITableService
    {
        //生成随机时间
        DateTime GenerateWithTime(DateTime startDate, DateTime endDate);
        //筛查
        string nczsc(List<RecordPatientrecordsg> patient,List<SiftStrokeup> nczsc);
        string gxysc(List<RecordPatientrecordsg> patient, List<SiftHypertension> gxysc);
        string tnbsc(List<RecordPatientrecordsg> patient, List<SiftDiabetes> tnbsc);
        string gxbsc(List<RecordPatientrecordsg> patient, List<SiftCoronary> gxbsc);
        string mzfsc(List<RecordPatientrecordsg> patient, List<SiftCopdup> mzfsc);
        string mxszsc(List<RecordPatientrecordsg> patient, List<SiftChronicKidneydisease> mxszsc);
    }
    public class TableService : ITableService, ITransient
    {
        private readonly ISqlSugarClient _db;
        private static readonly Random _random = new Random();
        public TableService(ISqlSugarClient db)
        {
            _db = db;
        }
        List<PatientExample> Mbdata = new List<PatientExample>
        {
            // 高血压 - 男
            new PatientExample { Disease = "高血压", Gender = "男", AgeRange = "50-55", Name = "邵元斌", IDNumber = "370921197106241811" },
            new PatientExample { Disease = "高血压", Gender = "男", AgeRange = "55-60", Name = "王光新", IDNumber = "370921196603065738" },
            new PatientExample { Disease = "高血压", Gender = "男", AgeRange = "60-65", Name = "周学连", IDNumber = "370921196111275712" },
            new PatientExample { Disease = "高血压", Gender = "男", AgeRange = "65-70", Name = "贺永祥", IDNumber = "370921195904093033" },
            new PatientExample { Disease = "高血压", Gender = "男", AgeRange = "70-75", Name = "李保山", IDNumber = "370921195405050098" },
            new PatientExample { Disease = "高血压", Gender = "男", AgeRange = "75-80", Name = "耿克娥", IDNumber = "370921194603100610" },
            new PatientExample { Disease = "高血压", Gender = "男", AgeRange = "80-80", Name = "赵宗芳", IDNumber = "370921194201120037" },
            
            // 高血压 - 女
            new PatientExample { Disease = "高血压", Gender = "女", AgeRange = "50-55", Name = "李爱君", IDNumber = "370921197103022162" },
            new PatientExample { Disease = "高血压", Gender = "女", AgeRange = "55-60", Name = "杨爱芹", IDNumber = "370921196812010346" },
            new PatientExample { Disease = "高血压", Gender = "女", AgeRange = "60-65", Name = "董新连", IDNumber = "37092119620620006x" },
            new PatientExample { Disease = "高血压", Gender = "女", AgeRange = "65-70", Name = "陆庆兰", IDNumber = "370921195706262123" },
            new PatientExample { Disease = "高血压", Gender = "女", AgeRange = "70-75", Name = "周桂玉", IDNumber = "370921195312305768" },
            new PatientExample { Disease = "高血压", Gender = "女", AgeRange = "75-80", Name = "李桂芝", IDNumber = "370921194805265763" },
            new PatientExample { Disease = "高血压", Gender = "女", AgeRange = "80-80", Name = "张桂芝", IDNumber = "370921194305130045" },
            
            // 糖尿病 - 男
            new PatientExample { Disease = "糖尿病", Gender = "男", AgeRange = "50-55", Name = "孔祥辉", IDNumber = "370921197404091532" },
            new PatientExample { Disease = "糖尿病", Gender = "男", AgeRange = "55-60", Name = "李恒忠", IDNumber = "370921196903295711" },
            new PatientExample { Disease = "糖尿病", Gender = "男", AgeRange = "60-65", Name = "张丛银", IDNumber = "370921196212062716" },
            new PatientExample { Disease = "糖尿病", Gender = "男", AgeRange = "65-70", Name = "王衍喜", IDNumber = "370921195610090013" },
            new PatientExample { Disease = "糖尿病", Gender = "男", AgeRange = "70-75", Name = "管振东", IDNumber = "370921195402032417" },
            new PatientExample { Disease = "糖尿病", Gender = "男", AgeRange = "75-80", Name = "王思明", IDNumber = "370921194803190059" },
            new PatientExample { Disease = "糖尿病", Gender = "男", AgeRange = "80-80", Name = "许玉英", IDNumber = "370921193612090018" },
            
            // 糖尿病 - 女
            new PatientExample { Disease = "糖尿病", Gender = "女", AgeRange = "50-55", Name = "刘丽香", IDNumber = "37029119730423302x" },
            new PatientExample { Disease = "糖尿病", Gender = "女", AgeRange = "55-60", Name = "许荣华", IDNumber = "370921197005182429" },
            new PatientExample { Disease = "糖尿病", Gender = "女", AgeRange = "60-65", Name = "高文静", IDNumber = "370921196407080068" },
            new PatientExample { Disease = "糖尿病", Gender = "女", AgeRange = "65-70", Name = "王爱芹", IDNumber = "370921196003120668" },
            new PatientExample { Disease = "糖尿病", Gender = "女", AgeRange = "70-75", Name = "迟玉艳", IDNumber = "370921195204030023" },
            new PatientExample { Disease = "糖尿病", Gender = "女", AgeRange = "75-80", Name = "胡爱英", IDNumber = "370921194802290322" },
            new PatientExample { Disease = "糖尿病", Gender = "女", AgeRange = "80-80", Name = "张秀兰", IDNumber = "37092119430814002x" },
            
            // 冠心病 - 男
            new PatientExample { Disease = "冠心病", Gender = "男", AgeRange = "50-55", Name = "赵存民", IDNumber = "370921197409232736" },
            new PatientExample { Disease = "冠心病", Gender = "男", AgeRange = "55-60", Name = "董先兵", IDNumber = "370921197005230910" },
            new PatientExample { Disease = "冠心病", Gender = "男", AgeRange = "60-65", Name = "苏伟臣", IDNumber = "37092119640630391x" },
            new PatientExample { Disease = "冠心病", Gender = "男", AgeRange = "65-70", Name = "张怀全", IDNumber = "37092119551222003x" },
            new PatientExample { Disease = "冠心病", Gender = "男", AgeRange = "70-75", Name = "王协林", IDNumber = "370921195210010934" },
            new PatientExample { Disease = "冠心病", Gender = "男", AgeRange = "75-80", Name = "张新平", IDNumber = "370921194703291514" },
            new PatientExample { Disease = "冠心病", Gender = "男", AgeRange = "80-80", Name = "安学山", IDNumber = "370921194308123318" },
            
            // 冠心病 - 女
            new PatientExample { Disease = "冠心病", Gender = "女", AgeRange = "50-55", Name = "张丽杰", IDNumber = "370921197111130040" },
            new PatientExample { Disease = "冠心病", Gender = "女", AgeRange = "55-60", Name = "王新梅", IDNumber = "370921196704130068" },
            new PatientExample { Disease = "冠心病", Gender = "女", AgeRange = "60-65", Name = "孙桂连", IDNumber = "37092119611102149" },  // 注意：原始数据身份证少1位，按原文保留
            new PatientExample { Disease = "冠心病", Gender = "女", AgeRange = "65-70", Name = "崔富荣", IDNumber = "370921195802065445" },
            new PatientExample { Disease = "冠心病", Gender = "女", AgeRange = "70-75", Name = "柳翠莲", IDNumber = "37092119491024004x" },
            new PatientExample { Disease = "冠心病", Gender = "女", AgeRange = "75-80", Name = "李秀芝", IDNumber = "370921194708030065" },
            new PatientExample { Disease = "冠心病", Gender = "女", AgeRange = "80-80", Name = "杨桂荣", IDNumber = "37092119440522062x" },
            
            // 脑卒中 - 男
            new PatientExample { Disease = "脑卒中", Gender = "男", AgeRange = "50-55", Name = "薛杰", IDNumber = "370921197007085745" },
            new PatientExample { Disease = "脑卒中", Gender = "男", AgeRange = "55-60", Name = "张荣富", IDNumber = "370921196711213611" },
            new PatientExample { Disease = "脑卒中", Gender = "男", AgeRange = "60-65", Name = "贾传海", IDNumber = "370921196202265712" },
            new PatientExample { Disease = "脑卒中", Gender = "男", AgeRange = "65-70", Name = "桑峰玉", IDNumber = "370921195706133612" },
            new PatientExample { Disease = "脑卒中", Gender = "男", AgeRange = "70-75", Name = "王顺先", IDNumber = "370921195305185710" },
            new PatientExample { Disease = "脑卒中", Gender = "男", AgeRange = "75-80", Name = "梁吉云", IDNumber = "37092119150905331x" },
            new PatientExample { Disease = "脑卒中", Gender = "男", AgeRange = "80-80", Name = "赵起海", IDNumber = "370921194111020315" },
            
            // 脑卒中 - 女
            new PatientExample { Disease = "脑卒中", Gender = "女", AgeRange = "50-55", Name = "任爱霞", IDNumber = "370830197606016523" },
            new PatientExample { Disease = "脑卒中", Gender = "女", AgeRange = "55-60", Name = "纪秀娥", IDNumber = "370921196512283625" },
            new PatientExample { Disease = "脑卒中", Gender = "女", AgeRange = "60-65", Name = "商秀芳", IDNumber = "370921196401060621" },
            new PatientExample { Disease = "脑卒中", Gender = "女", AgeRange = "65-70", Name = "张秀珍", IDNumber = "370921195511240629" },
            new PatientExample { Disease = "脑卒中", Gender = "女", AgeRange = "70-75", Name = "孙玉花", IDNumber = "370921195212153023" },
            new PatientExample { Disease = "脑卒中", Gender = "女", AgeRange = "75-80", Name = "张桂芳", IDNumber = "37092119460605092x" },
            new PatientExample { Disease = "脑卒中", Gender = "女", AgeRange = "80-80", Name = "高秀花", IDNumber = "370921194502140621" },
            
            // 慢阻肺 - 男
            new PatientExample { Disease = "慢阻肺", Gender = "男", AgeRange = "50-55", Name = "常吉路", IDNumber = "370921197102243617" },
            new PatientExample { Disease = "慢阻肺", Gender = "男", AgeRange = "55-60", Name = "徐永阳", IDNumber = "37092119671013003x" },
            new PatientExample { Disease = "慢阻肺", Gender = "男", AgeRange = "60-65", Name = "王常河", IDNumber = "370921196111080333" },
            new PatientExample { Disease = "慢阻肺", Gender = "男", AgeRange = "65-70", Name = "李英顺", IDNumber = "37092119570826005x" },
            new PatientExample { Disease = "慢阻肺", Gender = "男", AgeRange = "70-75", Name = "刘兴忠", IDNumber = "370921195001155416" },
            new PatientExample { Disease = "慢阻肺", Gender = "男", AgeRange = "75-80", Name = "王富先", IDNumber = "370921194708104212" },
            new PatientExample { Disease = "慢阻肺", Gender = "男", AgeRange = "80-80", Name = "崔衍道", IDNumber = "370921194306135710" },
            
            // 慢阻肺 - 女
            new PatientExample { Disease = "慢阻肺", Gender = "女", AgeRange = "50-55", Name = "柳召香", IDNumber = "370822197009215225" },
            new PatientExample { Disease = "慢阻肺", Gender = "女", AgeRange = "55-60", Name = "孙桂红", IDNumber = "370830197001186829" },
            new PatientExample { Disease = "慢阻肺", Gender = "女", AgeRange = "50-55", Name = "柳召香", IDNumber = "370822197009215225" },
            new PatientExample { Disease = "慢阻肺", Gender = "女", AgeRange = "55-60", Name = "孙桂红", IDNumber = "370830197001186829" },
            new PatientExample { Disease = "慢阻肺", Gender = "女", AgeRange = "60-65", Name = "刘风梅", IDNumber = "370921196110063929" },
            new PatientExample { Disease = "慢阻肺", Gender = "女", AgeRange = "65-70", Name = "朱月连", IDNumber = "370921195711183921" },
            new PatientExample { Disease = "慢阻肺", Gender = "女", AgeRange = "70-75", Name = "杨月香", IDNumber = "370921195001070922" },
            new PatientExample { Disease = "慢阻肺", Gender = "女", AgeRange = "75-80", Name = "袁井荣", IDNumber = "370921194801072745" },
            new PatientExample { Disease = "慢阻肺", Gender = "女", AgeRange = "80-80", Name = "鹿玉芳", IDNumber = "370921194207100029" },
            
            // 慢肾病 - 男
            new PatientExample { Disease = "慢肾病", Gender = "男", AgeRange = "50-55", Name = "许崇淇", IDNumber = "370921197209170016" },
            new PatientExample { Disease = "慢肾病", Gender = "男", AgeRange = "55-60", Name = "张培业", IDNumber = "370921196811160019" },
            new PatientExample { Disease = "慢肾病", Gender = "男", AgeRange = "60-65", Name = "黄庆臣", IDNumber = "370921196205060990" },
            new PatientExample { Disease = "慢肾病", Gender = "男", AgeRange = "65-70", Name = "刘庆金", IDNumber = "370921195507132713" },
            new PatientExample { Disease = "慢肾病", Gender = "男", AgeRange = "70-75", Name = "贺永社", IDNumber = "370921195504143011" },
            new PatientExample { Disease = "慢肾病", Gender = "男", AgeRange = "75-80", Name = "徐福廷", IDNumber = "370921194902283058" },
            new PatientExample { Disease = "慢肾病", Gender = "男", AgeRange = "80-80", Name = "何如水", IDNumber = "370921194503180916" },
            
            // 慢肾病 - 女
            new PatientExample { Disease = "慢肾病", Gender = "女", AgeRange = "50-55", Name = "宁鹏", IDNumber = "370921197404090089" },
            new PatientExample { Disease = "慢肾病", Gender = "女", AgeRange = "55-60", Name = "史爱玲", IDNumber = "370921196809273647" },
            new PatientExample { Disease = "慢肾病", Gender = "女", AgeRange = "60-65", Name = "施玉梅", IDNumber = "370921196112043649" },
            new PatientExample { Disease = "慢肾病", Gender = "女", AgeRange = "65-70", Name = "张翠兰", IDNumber = "370921195702180921" },
            new PatientExample { Disease = "慢肾病", Gender = "女", AgeRange = "70-75", Name = "王玉兰", IDNumber = "37092119531129362x" },
            new PatientExample { Disease = "慢肾病", Gender = "女", AgeRange = "75-80", Name = "赵培英", IDNumber = "370921195004185741" },
            new PatientExample { Disease = "慢肾病", Gender = "女", AgeRange = "80-80", Name = "刘德秀", IDNumber = "370921194407060025" }
        };
        //9男10女 脑卒中
        public string nczsc(List<RecordPatientrecordsg> patient, List<SiftStrokeup> nczsc)
        {
            int AgeEnd = 0;
            int AgeStart = 0;
            var nczdata=Mbdata.Select(t=>t.Disease).Distinct().ToList();
            foreach (var disease in nczdata) {
                var mddatas = Mbdata.Where(t => t.Disease == disease).ToList();
                foreach (var item in mddatas)
                {
                    //查询模版数据
                    if (item.Gender == "男")
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            SiftStrokeup sift = nczsc.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.SiftDate).ToList().FirstOrDefault();
                            if (sift!=null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.enrollments.Contains("27") && t.Sex == "9")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(itemw.createDate.ObjToDate(), itemw.createDate.ObjToDate().AddDays(20));
                                        sift.Phone = itemw.Number;
                                        sift.SiftDate = date;
                                        sift.PatientId = itemw.PatientId;
                                        sift.ScreeningCode = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(sift).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }                         

                        }
                    }
                    else
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            SiftStrokeup sift = nczsc.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.SiftDate).ToList().FirstOrDefault();
                            if (sift != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.enrollments.Contains("27") && t.Sex == "10")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(itemw.createDate.ObjToDate(), itemw.createDate.ObjToDate().AddDays(20));
                                        sift.Phone = itemw.Number;
                                        sift.SiftDate = date;
                                        sift.PatientId = itemw.PatientId;
                                        sift.ScreeningCode = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(sift).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }

                }
            }

            //var patients = patient.Where().tol;
            return "脑卒中筛查完成";
        }
        //高血压筛查
        public string gxysc(List<RecordPatientrecordsg> patient, List<SiftHypertension> gxysc)
        {
            int AgeEnd = 0;
            int AgeStart = 0;
            var nczdata = Mbdata.Select(t => t.Disease).Distinct().ToList();
            foreach (var disease in nczdata)
            {
                var mddatas = Mbdata.Where(t => t.Disease == disease).ToList();
                foreach (var item in mddatas)
                {
                    //查询模版数据
                    if (item.Gender == "男")
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            SiftHypertension sift = gxysc.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.SiftDate).ToList().FirstOrDefault();
                            if (sift != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.enrollments.Contains("24") && t.Sex == "9")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(itemw.createDate.ObjToDate(), itemw.createDate.ObjToDate().AddDays(20));
                                        sift.Number = itemw.Number;
                                        sift.SiftDate = date;
                                        sift.PatientId = itemw.PatientId;
                                        sift.ScreeningCode = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(sift).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }
                    else
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            SiftHypertension sift = gxysc.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.SiftDate).ToList().FirstOrDefault();
                            if (sift != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.enrollments.Contains("24") && t.Sex == "10")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(itemw.createDate.ObjToDate(), itemw.createDate.ObjToDate().AddDays(20));
                                        sift.Number = itemw.Number;
                                        sift.SiftDate = date;
                                        sift.PatientId = itemw.PatientId;
                                        sift.ScreeningCode = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(sift).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }

                }
            }

            //var patients = patient.Where().tol;
            return "高血压筛查完成";
        }
        //高血压筛查
        public string tnbsc(List<RecordPatientrecordsg> patient, List<SiftDiabetes> tnbsc)
        {
            int AgeEnd = 0;
            int AgeStart = 0;
            var nczdata = Mbdata.Select(t => t.Disease).Distinct().ToList();
            foreach (var disease in nczdata)
            {
                var mddatas = Mbdata.Where(t => t.Disease == disease).ToList();
                foreach (var item in mddatas)
                {
                    //查询模版数据
                    if (item.Gender == "男")
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            SiftDiabetes sift = tnbsc.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.SiftDate).ToList().FirstOrDefault();
                            if (sift != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.enrollments.Contains("25") && t.Sex == "9")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(itemw.createDate.ObjToDate(), itemw.createDate.ObjToDate().AddDays(20));
                                        sift.Phone = itemw.Number;
                                        sift.SiftDate = date;
                                        sift.PatientId = itemw.PatientId;
                                        sift.ScreeningCode = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(sift).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }
                    else
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            SiftDiabetes sift = tnbsc.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.SiftDate).ToList().FirstOrDefault();
                            if (sift != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.enrollments.Contains("25") && t.Sex == "10")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(itemw.createDate.ObjToDate(), itemw.createDate.ObjToDate().AddDays(20));
                                        sift.Phone = itemw.Number;
                                        sift.SiftDate = date;
                                        sift.PatientId = itemw.PatientId;
                                        sift.ScreeningCode = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(sift).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }

                }
            }

            //var patients = patient.Where().tol;
            return "糖尿病筛查完成";
        }
        public string gxbsc(List<RecordPatientrecordsg> patient, List<SiftCoronary> gxbsc)
        {
            int AgeEnd = 0;
            int AgeStart = 0;
            var nczdata = Mbdata.Select(t => t.Disease).Distinct().ToList();
            foreach (var disease in nczdata)
            {
                var mddatas = Mbdata.Where(t => t.Disease == disease).ToList();
                foreach (var item in mddatas)
                {
                    //查询模版数据
                    if (item.Gender == "男")
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            SiftCoronary sift = gxbsc.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.SiftDate).ToList().FirstOrDefault();
                            if (sift != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.enrollments.Contains("26") && t.Sex == "9")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(itemw.createDate.ObjToDate(), itemw.createDate.ObjToDate().AddDays(20));
                                        sift.Number = itemw.Number;
                                        sift.SiftDate = date;
                                        sift.PatientId = itemw.PatientId;
                                        sift.ScreeningCode = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(sift).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }
                    else
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            SiftCoronary sift = gxbsc.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.SiftDate).ToList().FirstOrDefault();
                            if (sift != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.enrollments.Contains("26") && t.Sex == "10")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(itemw.createDate.ObjToDate(), itemw.createDate.ObjToDate().AddDays(20));
                                        sift.Number = itemw.Number;
                                        sift.SiftDate = date;
                                        sift.PatientId = itemw.PatientId;
                                        sift.ScreeningCode = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(sift).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }

                }
            }

            //var patients = patient.Where().tol;
            return "冠心病筛查完成";
        }
        public string mzfsc(List<RecordPatientrecordsg> patient, List<SiftCopdup> mzfsc)
        {
            int AgeEnd = 0;
            int AgeStart = 0;
            var nczdata = Mbdata.Select(t => t.Disease).Distinct().ToList();
            foreach (var disease in nczdata)
            {
                var mddatas = Mbdata.Where(t => t.Disease == disease).ToList();
                foreach (var item in mddatas)
                {
                    //查询模版数据
                    if (item.Gender == "男")
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            SiftCopdup sift = mzfsc.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.SiftDate).ToList().FirstOrDefault();
                            if (sift != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.enrollments.Contains("28") && t.Sex == "9")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(itemw.createDate.ObjToDate(), itemw.createDate.ObjToDate().AddDays(20));
                                        sift.Phone = itemw.Number;
                                        sift.SiftDate = date;
                                        sift.PatientId = itemw.PatientId;
                                        sift.ScreeningCode = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(sift).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }
                    else
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            SiftCopdup sift = mzfsc.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.SiftDate).ToList().FirstOrDefault();
                            if (sift != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.enrollments.Contains("28") && t.Sex == "10")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(itemw.createDate.ObjToDate(), itemw.createDate.ObjToDate().AddDays(20));
                                        sift.Phone = itemw.Number;
                                        sift.SiftDate = date;
                                        sift.PatientId = itemw.PatientId;
                                        sift.ScreeningCode = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(sift).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }

                }
            }

            //var patients = patient.Where().tol;
            return "慢阻肺筛查完成";
        }
        public string mxszsc(List<RecordPatientrecordsg> patient, List<SiftChronicKidneydisease> mxszsc)
        {
            int AgeEnd = 0;
            int AgeStart = 0;
            var nczdata = Mbdata.Select(t => t.Disease).Distinct().ToList();
            foreach (var disease in nczdata)
            {
                var mddatas = Mbdata.Where(t => t.Disease == disease).ToList();
                foreach (var item in mddatas)
                {
                    //查询模版数据
                    if (item.Gender == "男")
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            SiftChronicKidneydisease sift = mxszsc.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.SiftDate).ToList().FirstOrDefault();
                            if (sift != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.enrollments.Contains("29") && t.Sex == "9")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(itemw.createDate.ObjToDate(), itemw.createDate.ObjToDate().AddDays(20));
                                        sift.Phone = itemw.Number;
                                        sift.SiftDate = date;
                                        sift.PatientId = itemw.PatientId;
                                        sift.ScreeningCode = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(sift).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }
                    else
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            SiftChronicKidneydisease sift = mxszsc.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.SiftDate).ToList().FirstOrDefault();
                            if (sift != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.enrollments.Contains("29") && t.Sex == "10")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(itemw.createDate.ObjToDate(), itemw.createDate.ObjToDate().AddDays(20));
                                        sift.Phone = itemw.Number;
                                        sift.SiftDate = date;
                                        sift.PatientId = itemw.PatientId;
                                        sift.ScreeningCode = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(sift).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }

                }
            }

            //var patients = patient.Where().tol;
            return "慢性肾脏筛查完成";
        }

        /// <summary>
        /// 生成带随机时间的日期（精确到秒）
        /// </summary>
        public DateTime GenerateWithTime(DateTime startDate, DateTime endDate)
        {
            if (endDate < startDate)
            {
                throw new ArgumentException("结束日期不能早于开始日期");
            }

            // 计算时间范围（毫秒数）
            TimeSpan timeSpan = endDate - startDate;
            double totalMilliseconds = timeSpan.TotalMilliseconds;

            // 生成随机毫秒数
            double randomMilliseconds = _random.NextDouble() * totalMilliseconds;

            // 返回带时间的随机日期
            return startDate.AddMilliseconds(randomMilliseconds);
        }

        public string sfPlan(List<RecordPatientrecordsg> patient, List<FollowPlan> plan)
        {
            int AgeEnd = 0;
            int AgeStart = 0;
            var nczdata = Mbdata.Select(t => t.Disease).Distinct().ToList();
            foreach (var disease in nczdata)
            {
                var mddatas = Mbdata.Where(t => t.Disease == disease).ToList();
                foreach (var item in mddatas)
                {
                    //查询模版数据
                    if (item.Gender == "男")
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            FollowPlan follow = plan.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.FollowUpDate).ToList().FirstOrDefault();
                            if (follow != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.Sex == "9")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(follow.FollowUpDate.ObjToDate(), follow.FollowUpDate.ObjToDate().AddDays(5));
                                        
                                        follow.FollowUpDate = date.ToString();
                                        follow.PatientId = itemw.PatientId;
                                        follow.PlanNo = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(follow).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }
                    else
                    {
                        RecordPatientrecordsg patients = patient.Where(t => t.IdCard == item.IDNumber).ToList().FirstOrDefault();
                        if (patients != null)
                        {
                            var Age = item.AgeRange.Split("-");
                            AgeEnd = Age[0].ObjToInt();
                            AgeStart = Age[1].ObjToInt();
                            DateTime? startBirthDate = AgeEnd != null ? DateTime.Now.AddYears(-AgeStart) : (DateTime?)null;
                            DateTime? endBirthDate = AgeStart != null ? DateTime.Now.AddYears(-AgeEnd).AddDays(1) : (DateTime?)null;
                            FollowPlan follow = plan.Where(t => t.PatientId == patients.PatientId).OrderByDescending(t => t.FollowUpDate).ToList().FirstOrDefault();
                            if (follow != null)
                            {
                                //获取符合年龄条件的病人信息模版
                                var patientw = patient
                                    .WhereIF(startBirthDate == endBirthDate, t => t.Birth >= startBirthDate.ObjToDate())
                                    .WhereIF(startBirthDate != endBirthDate, t => t.Birth >= startBirthDate.ObjToDate() && t.Birth <= endBirthDate.ObjToDate())
                                    .Where(t => t.Sex == "9")
                                    .ToList();
                                if (patientw.Count > 0)
                                {
                                    foreach (var itemw in patientw)
                                    {
                                        var date = GenerateWithTime(follow.FollowUpDate.ObjToDate(), follow.FollowUpDate.ObjToDate().AddDays(5));

                                        follow.FollowUpDate = date.ToString();
                                        follow.PatientId = itemw.PatientId;
                                        follow.PlanNo = "S" + date.ToString("yyyyMMddHHmmss") + _random.Next(1000, 9999);
                                        var isok = _db.Insertable(follow).ExecuteCommandIdentityIntoEntity();
                                    }
                                }
                            }

                        }
                    }

                }
            }
            return "高血压随访";
        }
    }
}
